package com.smscenter.server.core.response;

import java.util.HashMap;
import  java.util.Map;

import com.zeus.base.smscenter.common.util.XMLParser;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
 * Created by Administrator on 2016-02-20.
 */
public class EmsReponse {

    private static Map<String,String> response=new HashMap<String, String>();

    private static EmsReponse instance = new EmsReponse();

    private EmsReponse (){
         response.put("0","SUCCESS");
         response.put("1","登录密码错误");
         response.put("2","企业ID或登录名错误");
         response.put("3","余额不足");
         response.put("4","用户归属服务器错误");
         response.put("5","帐户停用或不存在");
         response.put("6","内容为空");
         response.put("7","号码为空");
         response.put("8","号码超过最大限制数");
         response.put("9","内容包含关键字");
         response.put("10","时间格式错误");
         response.put("11","非法操作导致ip被锁");
         response.put("12","访问过快");
         response.put("13","批量一对一参数格式错误");
         response.put("14","批量一对一出现重复信息(号码，内容同时重复)");
         response.put("15","签名未报备");
         response.put("16","单位时间内该号码超过最大发送限制");
         response.put("17","签名必须在【4，10】字符间");
         response.put("18","内容涉嫌营销内容");
         response.put("99","系统内部错误");
         response.put("-9999","咱们系统内部错误");
    }

    public static EmsReponse getInstance() {
        return instance;
    }

    public String getResponseMessage(String code){
        return response.get(parseXML(code).get("state"));
    }
    
    private Map<String,String> parseXML(String xml) {
		Map<String,String> map = new HashMap();
		map.put("state", "-9999");
		try {
			// 解析返回XML字符串
			XMLParser parser = XMLParser.getInstance();
			parser.parse(xml);
			NodeList rootList = parser.getList(parser.getDoc(), "Response");
			for (int i = 0; i < rootList.getLength(); i++) {
				Node root = rootList.item(i);
				String state = parser.getString(root, "Result");// 状态码
				map.put("state", state);
			}
		} catch (Exception e) {
			try {
				throw new Exception("解析XML失败");
			} catch (Exception e1) {
				e1.printStackTrace();
				return map;
			}
		}
		return map;
	}

}
